import { MutationTree } from 'vuex';
import { AppState } from './state';
import { AppMutationTypes } from './types';
import { setSidebarStatus } from '@/utils/cookies';

export type Mutations<S = AppState> = {
  [AppMutationTypes.TOGGLE_SIDEBAR](state: S): void,
  [AppMutationTypes.SET_ACTIVE_MENU](state: S, activeMenu: string): void,
}

export const mutations: MutationTree<AppState> & Mutations = {
  [AppMutationTypes.TOGGLE_SIDEBAR](state: AppState) {
    state.sidebar.opened = !state.sidebar.opened;
    if (state.sidebar.opened) {
      setSidebarStatus('opened');
    } else {
      setSidebarStatus('closed');
    }
  },
  [AppMutationTypes.SET_ACTIVE_MENU](state: AppState, activeMenu: string) {
    state.sidebar.activeMenu = activeMenu;
  },
};
